package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/admin_deleteUser")
public class adminDeleteUser extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取参数
        String id = req.getParameter("id");
        String cp = req.getParameter("cp");
        String cz = req.getParameter("cz");

        if(Integer.parseInt(cz) == 2){
            //删除该用户
            boolean result = AdminDao.deleteUser(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('删除失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        }else if(Integer.parseInt(cz) == 3){
            //通过注册
            boolean result = AdminDao.allowUser(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('通过失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        }else if(Integer.parseInt(cz) == 1){
            //解封
            boolean result = AdminDao.allowUser(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('解封失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        }else if(Integer.parseInt(cz) == 5){
            //设为管理员
            boolean result = AdminDao.setAmdin(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('设置失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        }else if(Integer.parseInt(cz) == 6){
            //设为管理员
            boolean result = AdminDao.setAmdin1(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('设置失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        } else {
            //封号
            boolean result = AdminDao.closewUser(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_index?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('封号失败');location.href='/admin/admin_index"+cp+"';</script>");
            }
        }


    }
}
